package handlers

import (
	"context"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	"github.com/cloudwego/hertz/pkg/common/utils"
	"login/src/core/consts"
	net2 "login/src/core/net"
	"login/src/services"
	"net/http"
)

func LoginHandler(ctx context.Context, c *app.RequestContext) {
	var req net2.LoginReq
	if err := c.BindAndValidate(&req); err != nil {
		c.JSON(http.StatusOK, utils.H{
			"message": err.Error(),
			"code":    http.StatusBadRequest,
		})
		return
	}

	hlog.Infof("req:%v", req)

	switch consts.PlatformID(req.PlatformID) {
	case consts.PlatformOfficial:
		if consts.LoginOfficialGuest == consts.LoginType(req.LoginType) {
			_, err := services.GuestLogin(ctx, c, &req)
			if err != nil {
				return
			}
		} else if consts.LoginOfficialAccount == consts.LoginType(req.LoginType) {
		}
	case consts.PlatformGooglePlay:
		_, err := services.GoogleLogin(ctx, c, &req)
		if err != nil {
			return
		}
	case consts.PlatformApple:
		_, err := services.AppleLogin(ctx, c, &req)
		if err != nil {
			return
		}
	default:
		hlog.Errorf("unhandled default case,platformId:%v", req.PlatformID)
		net2.Failed(c, net2.StatusErrorPlatform, "unhandled default case,platformId")
	}
}
